﻿Imports TBCC.UtilityMobile
Public Class DialForm

    Private _ComNo As String '端口号
    Private _DialNumber As String '拨号号码
    Public Sub New(ByVal ComNo As String, ByVal DialNumber As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        _ComNo = ComNo
        _DialNumber = DialNumber
        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    ''' <summary>
    ''' Form Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub DialForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadCOM()

        LoadNumber()

    End Sub

    ''' <summary>
    ''' 加载串口号
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadCOM()
        For Each s As String In System.IO.Ports.SerialPort.GetPortNames
            Me.cboCOM.Items.Add(s)
        Next

        Me.cboCOM.SelectedIndex = Me.cboCOM.FindStringExact(_ComNo)

        If Me.cboCOM.SelectedIndex = -1 Then '配置的串口不存在
            msg.Show(String.Format("配置的{0}不存在", _ComNo))
            btnDia.Enabled = False
        End If

        Me.cboCOM.DropDownStyle = ComboBoxStyle.DropDownList
    End Sub

    ''' <summary>
    ''' 加载电话号码
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadNumber()
        Me.txtPhone.Text = Trim(_DialNumber)
    End Sub

    Private _cancled As Boolean = False

    Private Sub btnDia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDia.Click
        'Me.btnDia.Enabled = False
        If Me.btnDia.Text = "取消" Then
            CancelDia()
            Exit Sub
        Else
            btnDia.Text = "取消"
            Me.tspb1.Visible = True
        End If

        _cancled = False

        Me.btnClose.Enabled = False

        Application.DoEvents()
        Me.Refresh()

        Me.Timer1.Enabled = True

        'Me.Cursor = Cursors.WaitCursor
        Try
            Me.tsslDialStatus.Text = "初始化串口。"
            Application.DoEvents()
            Me.Refresh()

            COMHelper.Instance.InitSerialPort(Me.cboCOM.Text.Replace("COM", ""), IO.Ports.Parity.None)

            Me.tsslDialStatus.Text = "正在拨号..."
            If COMHelper.Instance.InitModem(Me.txtPhone.Text.Trim) = True Then
                'Me.Cursor = Cursors.Default
                'MyReader.Instance.StartRead()

                Dim strResult As String
                strResult = COMHelper.Instance.WriteCommand(UtilityClass.GetAuthCodeByte(MainForm.pi.AuthCode), 10, 10000)
                If strResult.IndexOf("AT^AUTH=OK") >= 0 Then
                    Me.DialogResult = Windows.Forms.DialogResult.OK
                    Me.Close()
                Else
                    Me.tsslDialStatus.Text = "与下位机的连接鉴权失败"
                    Me.btnDia.Text = "重拨"
                End If

                'Me.DialogResult = Windows.Forms.DialogResult.OK
                'Me.Close()
            Else
                Me.tsslDialStatus.Text = "拨号超时:拨号无应答"
                Me.btnDia.Text = "重拨"
            End If
        Catch ex As Exception
            Dim ep As New Dictionary(Of String, Object)
            ep.Add("phoneNumber", Me.txtPhone.Text)
            'Logger.WriteWarning(ex, "拨号失败", ep)

            If _cancled Then
                Me.tsslDialStatus.Text = ""
            Else
                Select Case ex.Message
                    Case "端口被关闭。"
                        Me.tsslDialStatus.Text = "串口被占用"
                    Case "BUSY"
                        Me.tsslDialStatus.Text = "对方占线"
                    Case "ERROR"
                        Me.tsslDialStatus.Text = "拨号失败"
                    Case "NO DIALTONE"
                        Me.tsslDialStatus.Text = "无拨号音"
                    Case Else
                        Me.tsslDialStatus.Text = ex.Message
                End Select

            End If

            Me.btnDia.Text = "重拨"
        Finally
            'Me.Cursor = Cursors.Default
        End Try

        Me.tspb1.Visible = False

        Me.btnDia.Enabled = True
        Me.btnClose.Enabled = True
        Me.Timer1.Stop()
        Me.tspb1.Value = 0
    End Sub

    Private Sub CancelDia()
        COMHelper.Instance.Close()
        System.Threading.Thread.Sleep(100)
        Application.DoEvents()

        Me.btnDia.Text = "拨号"
        Me.tsslDialStatus.Text = ""
        Me.btnClose.Enabled = True
        Me.Cursor = Cursors.Default

        Me.tspb1.Visible = False
        _cancled = True
    End Sub

    Private bFirst As Boolean = True

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'If bFirst Then
        '    bFirst = False
        '    btnDia.PerformClick()
        '    Me.tspb1.Value = 0
        '    Exit Sub
        'End If

        If tspb1.Value = tspb1.Maximum Then
            tspb1.Value = 0
        End If

        Me.tspb1.Value += 1
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Timer1.Stop()

        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        'Me.Close()
    End Sub

End Class